package com.qf.web.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.service.impl.*;
import com.qf.utils.StringUtils;
import com.qf.web.common.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/customerservlet")
public class CustomServlet extends BaseServlet {
    CustomerService customerService =new CustomerServiceImpl();

    TeamService teamService = new TeamServiceImpl();

    CustypeService custypeService =new CustypeServiceImpl();

    CusstateService cusstateService =new CustateServiceImpl();

    CustagService custagService =new CustagServiceImpl();

    EmpService empService = new EmpServiceImpl();

    protected String listAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取页码
        PageBean<Customer> pageBean ;
        String pageNum = req.getParameter("pageNum");
        //获取也大小 每页显示条数
        String pageSize = req.getParameter("pageSize");
        //校验
        int page_num = 1; //默认从第一页开始
        int page_size = 10;//默认每页显示8条数据
        if (!StringUtils.isEmpty(pageNum)) {//如果前端给我们页码
            page_num = Integer.parseInt(pageNum);
            if (page_num < 0) {
                page_num = 1;
            }
        }
        if (!StringUtils.isEmpty(pageSize)) {//如果前端给我们页码
            page_size = Integer.parseInt(pageSize);
            if (page_size < 0) {
                page_size = 10;
            }
        }
        //调用业务逻辑

        pageBean = customerService.findByPage(page_size, page_num);

        //存储到请求域
        req.setAttribute("totalCount",pageBean.getTotalCount());
        req.setAttribute("list", pageBean.getList());
        req.setAttribute("pageNum",pageBean.getPageNum());

        return "forward:/pages/crm/customer/list.jsp";
    }
    protected String selectList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object users = req.getSession().getAttribute("users");
        if (users == null) {
            return "redirect:/pages/login.jsp";
        }
        List<Team> teamList = teamService.findAllTeam();
        List<Custype> custypeList = custypeService.findAllCustype();
        List<Cusstate> cusstateList = cusstateService.findAllCusstate();
        List<Custag> custagList = custagService.findAllCustag();
        req.setAttribute("teamlist", teamList);
        req.setAttribute("typelist", custypeList);
        req.setAttribute("statelist", cusstateList);
        req.setAttribute("taglist", custagList);
        return "forward:/pages/crm/customer/add.jsp";
    }
    protected String addlist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cusName = req.getParameter("cus_name");
        int cusAge = Integer.parseInt(req.getParameter("cus_age"));
        String tel = req.getParameter("tel");
        String cusLocation = req.getParameter("cus_location");
        String cusCompany = req.getParameter("cus_company");
        String cusPosition = req.getParameter("cus_position");
        int teamId = Integer.parseInt(req.getParameter("team_id"));
        int cusTypeId = Integer.parseInt(req.getParameter("cus_type_id"));
        int stateId = Integer.parseInt(req.getParameter("state_id"));
        int tagId = Integer.parseInt(req.getParameter("tag_id"));
        int empId = Integer.parseInt(req.getParameter("emp_id"));
        String json = null;
        Customer customer = new Customer(1, cusName, cusTypeId, tagId, stateId, tel, empId, teamId, cusAge, cusLocation, cusCompany, cusPosition);
        try {
            customerService.addlist(customer);
            AjaxMsgModel ajaxMsgModel = new AjaxMsgModel("200", "操作成功", "main", "", "closeCurrent", "/customerservlet?method=listAll");
            json = JSON.toJSONString(ajaxMsgModel);
        } catch (Exception e) {
            AjaxMsgModel ajaxMsgModel = AjaxMsgModel.createfailModel(false);
            json = JSON.toJSONString(ajaxMsgModel);
        }
        return json;

    }

    public String findEmplist(HttpServletRequest req, HttpServletResponse resp) {
        String teamId = req.getParameter("teamId");
        List<Emp> emplist = empService.findEmplist(teamId);
        String data = JSONObject.toJSONString(emplist);
        return data;
    }
    protected String updataList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cusId = req.getParameter("cusId");
        try {
            List<Team> teamList = teamService.findAllTeam();
            List<Custype> custypeList = custypeService.findAllCustype();
            List<Cusstate> cusstateList = cusstateService.findAllCusstate();
            List<Custag> custagList = custagService.findAllCustag();
            req.setAttribute("teamlist", teamList);
            req.setAttribute("typelist", custypeList);
            req.setAttribute("statelist", cusstateList);
            req.setAttribute("taglist", custagList);
            Customer customer = customerService.findById(cusId);
            req.setAttribute("customer", customer);
            return "forward:/pages/crm/customer/update.jsp";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public String doUpdate(HttpServletRequest request, HttpServletResponse response) {
        int cusId = Integer.parseInt(request.getParameter("cusId"));
        String cusName = request.getParameter("cusName");
        int cusAge = Integer.parseInt(request.getParameter("cusAge"));
        String tel = request.getParameter("tel");
        String cusLocation = request.getParameter("cusLocation");
        String cusCompany = request.getParameter("cusCompany");
        String cusPosition = request.getParameter("cusPosition");
        int teamId = Integer.parseInt(request.getParameter("teamId"));
        int empId = Integer.parseInt(request.getParameter("empId"));
        int cusTypeId = Integer.parseInt(request.getParameter("cusTypeId"));
        int stateId = Integer.parseInt(request.getParameter("stateId"));
        int tagId = Integer.parseInt(request.getParameter("tagId"));
        Customer customer = new Customer(cusId, cusName, cusTypeId, tagId, stateId, tel, empId, teamId, cusAge, cusLocation, cusCompany, cusPosition);
        String json = null;
        try {
            customerService.doUpdate(customer);
            AjaxMsgModel ajaxMsgModel = new AjaxMsgModel("200", "操作成功", "main", "", "closeCurrent", "/customerservlet?method=listAll");
            json = JSON.toJSONString(ajaxMsgModel);
        } catch (Exception e) {
            AjaxMsgModel ajaxMsgModel = AjaxMsgModel.createfailModel(false);
            json = JSON.toJSONString(ajaxMsgModel);
        }
        return json;
    }
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    }


